/**
 * @Copyright ®2015 Sinosoft Co. Ltd. All rights reserved.
 * 项目名称 : 网站服务平台
 * 创建日期 : 2016-11-23
 * 修改历史 :
 * 1. [2016-11-23]创建文件 by chenxin
 */
package com.sinosoft.sinopay.service.merchant.biz.service.impl;

import com.sinosoft.sinocloud.platform.base.SinoBaseService;
import com.sinosoft.sinocloud.platform.common.web.pagequery.PageGrid;
import com.sinosoft.sinocloud.platform.common.web.pagequery.PageQueryModel;
import com.sinosoft.sinopay.merchants.dto.MerchantDTO;
import com.sinosoft.sinopay.merchants.dto.MerchantSignInfo;
import com.sinosoft.sinopay.merchants.dto.PayMerchantDTO;
import com.sinosoft.sinopay.merchants.dto.RelaThirdpayDTO;
import com.sinosoft.sinopay.service.merchant.biz.integration.mapper.MerchantMapper;
import com.sinosoft.sinopay.service.merchant.biz.integration.dao.MerchantPageMapper;
import com.sinosoft.sinopay.service.merchant.biz.service.MerchantInfoCoreService;
import com.sinosoft.sinopay.service.merchant.generated.mapper.PayMerchantMapper;
import com.sinosoft.sinopay.service.merchant.generated.mapper.RelaMerchantThirdPayMineMapper;
import com.sinosoft.sinopay.service.merchant.generated.model.PayMerchant;
import com.sinosoft.sinopay.service.merchant.generated.model.PayMerchantExample;
import com.sinosoft.sinopay.service.merchant.generated.model.RelaMerchantThirdPayMine;
import com.sinosoft.sinopay.service.merchant.generated.model.RelaMerchantThirdPayMineExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 【商户信息服务接口实现类】
 * @author chenxin
 * @date 2016-11-23 上午11:17 
 * @version V1.0
 */
@Service
public class MerchantInfoCoreServiceImpl extends SinoBaseService implements MerchantInfoCoreService{

    @Autowired
    private MerchantMapper merchantMapper;

    @Autowired
    private PayMerchantMapper payMerchantMapper;

    @Autowired
    private RelaMerchantThirdPayMineMapper relaMerchantThirdPayMineMapper;

    @Autowired
    private MerchantPageMapper merchantPageMapper;

    /**
     * 获取全部的商户签名信息
     * @return   key为商户code，value为商户signkey
     */
    @Override
    public  Map<String,String>  getAllMerchantSignKeys() {
        List<Map<String,String>> allMerchant = merchantMapper.selectAllMerchantSignKeys();
        Map<String,String> resultMap = new HashMap();
        for(int i = 0; i < allMerchant.size() ; i++){
            resultMap.put(allMerchant.get(i).get("merchantCode"),allMerchant.get(i).get("signKey"));
        }
        return resultMap;
    }

    /**
     * 根据页码和参数获取当页商户信息集合
     * @return
     */
    @Override
    public PageGrid getMerchantMsgForPageAndDate(PageQueryModel pageQueryModel){
        return merchantPageMapper.selectMerchantMsgForPageAndDate(pageQueryModel);
    }


    /**
     * 根据sid查询商户详情信息
     * @return  商户详情信息
     */
    @Override
    public MerchantSignInfo getMerchantForSid(Integer sid){
        return merchantMapper.selectMerchantForSid(sid);
    }

    /**
     * 新增商户信息
     * @return  商户信息Sid
     */
    @Override
    public String addMerchantMsg(PayMerchantDTO payMerchant){
        String merchantName = null;
        logger.info("========start addMerchantMsg========");
        if(payMerchant != null){
            merchantName = insertMerchantMsg(payMerchant);
            logger.info("========end addMerchantMsg========");
        }else{
            merchantName = "0";
        }
        return merchantName;
    }

    /**
     * 更新商户信息
     * @return  true = 更新成功; false = 更新失败
     */
    @Override
    public String updateMerchantMsg(PayMerchantDTO payMerchant){
        String result = payMerchant.getMerchantName();
        //获取需要修改的Sid
        Integer sid =  payMerchant.getSid();
        Integer thirdPaySid = payMerchant.getThirdpaySid();
        PayMerchantExample payMerchantExample = new PayMerchantExample();
        RelaMerchantThirdPayMineExample relaMerchantThirdPayMineExample = new RelaMerchantThirdPayMineExample();

        //根据sid判断该条商户信息是否为空
        payMerchantExample.createCriteria().andSidEqualTo(sid);
        relaMerchantThirdPayMineExample.createCriteria().andSidEqualTo(thirdPaySid);
        List<PayMerchant> payMerchantList = payMerchantMapper.selectByExample(payMerchantExample);
        List<RelaMerchantThirdPayMine> relaMerchantThirdPayMineList = relaMerchantThirdPayMineMapper.selectByExample(relaMerchantThirdPayMineExample);

        if((payMerchantList != null && payMerchantList.size() > 0) && (relaMerchantThirdPayMineList != null && relaMerchantThirdPayMineList.size() > 0)){
            /* 获取数据库中已存在的商户信息,把更改的信息写入数据 */
            PayMerchant payMerchants   = payMerchantList.get(0);
            payMerchants.setMerchantName(payMerchant.getMerchantName());
            payMerchants.setPayBackNotifyurl(payMerchant.getPayBackNotifyurl());
            payMerchants.setPayPageNotifyurl(payMerchant.getPayPageNotifyurl());
            payMerchants.setModifierId(payMerchant.getModifierId());
            payMerchants.setModifyDate(new Date());
            payMerchants.setMerchantType(payMerchant.getMerchantType());
            payMerchants.setShortName(payMerchant.getShortName());
            payMerchants.setStatus(payMerchant.getStatus());
            payMerchantMapper.updateByPrimaryKeySelective(payMerchants);

            /* 获取数据库中已存在的商户与支付渠道关系信息,把更改的信息写入数据 */
            RelaMerchantThirdPayMine relaMerchantThirdPayMine = relaMerchantThirdPayMineList.get(0);
            /* 修改支付渠道状态 */
            relaMerchantThirdPayMine.setModifierId(payMerchant.getModifierId());
            relaMerchantThirdPayMine.setState(payMerchant.getNexusStatus());
            if(payMerchant.getRelaThirdpayList() != null && payMerchant.getRelaThirdpayList().size() != 0  ) {
                RelaThirdpayDTO relaThirdpay = payMerchant.getRelaThirdpayList().get(0);
                relaMerchantThirdPayMine.setThirdpayName(relaThirdpay.getThirdpayName());
            }
            relaMerchantThirdPayMineMapper.updateByPrimaryKeySelective(relaMerchantThirdPayMine);
        }else{
            result = "0";//失败
        }
        return result;
    }

    /**
     * 删除商户信息
     * @return  true = 删除成功; false = 删除失败
     */
    @Override
    public boolean deleteMerchantMsg(Integer sid){
        boolean result = false;
        PayMerchantExample payMerchantExample = new PayMerchantExample();
        //根据sid判断该条商户信息是否为空
        payMerchantExample.createCriteria().andSidEqualTo(sid);
        List<PayMerchant> payMerchantList = payMerchantMapper.selectByExample(payMerchantExample);
        if(payMerchantList != null && payMerchantList.size() > 0){
            PayMerchant payMerchants   = payMerchantList.get(0);
            //将status状态改为0 停用状态
            payMerchants.setStatus("0");
            payMerchantMapper.updateByPrimaryKeySelective(payMerchants);
            result = true;
        }
        return result;
    }


    /**
     * 向商户信息表中插入一条数据
     * @return  商户信息Sid
     */
    public String insertMerchantMsg(PayMerchantDTO payMerchant){
        String merchantName = null;
            if(payMerchant != null){
                PayMerchant payMerchants = new PayMerchant();
                /* 写入商户表 */
                payMerchants.setMerchantCode(payMerchant.getMerchantCode());
                payMerchants.setMerchantName(payMerchant.getMerchantName());
                payMerchants.setShortName(payMerchant.getShortName());
                payMerchants.setMerchantType(payMerchant.getMerchantType());
                payMerchants.setSignKey(payMerchant.getSignKey());
                payMerchants.setStatus(payMerchant.getStatus());
                payMerchants.setCreatorId(payMerchant.getCreatorId());
                payMerchants.setCreateDate(new Date());
                payMerchants.setModifierId(payMerchant.getCreatorId());
                payMerchants.setModifyDate(new Date());
                payMerchantMapper.insert(payMerchants);

                /* 写入商户与渠道关系表 */
                if(payMerchant.getRelaThirdpayList() != null && payMerchant.getRelaThirdpayList().size() != 0) {
                    for(int i = 0 ; i < payMerchant.getRelaThirdpayList().size() ; i++) {
                        RelaThirdpayDTO relaThirdpay = payMerchant.getRelaThirdpayList().get(i);
                        RelaMerchantThirdPayMine relaMerchantThirdPayMine = new RelaMerchantThirdPayMine();
                        relaMerchantThirdPayMine.setThirdpayId(relaThirdpay.getThirdpayId());
                        relaMerchantThirdPayMine.setThirdpayName(relaThirdpay.getThirdpayName());
                        relaMerchantThirdPayMine.setMerchantId(payMerchants.getSid());
                        relaMerchantThirdPayMine.setMerchantName(payMerchant.getMerchantName());
                        relaMerchantThirdPayMine.setCreatorId(payMerchant.getCreatorId());
                        relaMerchantThirdPayMine.setState("1");
                        relaMerchantThirdPayMine.setCreateDate(new Date());
                        relaMerchantThirdPayMine.setModifierId(payMerchant.getCreatorId());
                        relaMerchantThirdPayMine.setModifyDate(new Date());
                        relaMerchantThirdPayMineMapper.insert(relaMerchantThirdPayMine);
                    }
                }
                merchantName = payMerchant.getMerchantName();
            }
        return merchantName;
    }

    /**
     *查询所有商户名和商户编码
     *@return  商户名和商户编码
     */
    public List<MerchantDTO> selectMerchantName(){
        return merchantMapper.selectMerchantName();
    }

    /**
     *根据商户CODE查询商户信息
     *@return 商户详情信息
     */
    public MerchantSignInfo selectMerchantByCode(String merchantCode){ return merchantMapper.selectMerchantByCode(merchantCode); }
}
